package com.lanou.zy.projecta.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.util.DataChange;
import com.lanou.zy.projecta.volley.VolleySingleton;
import com.lanou.zy.projecta.entity.Latest;

import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class LatestRecyclerViewAdapter extends RecyclerView.Adapter<LatestRecyclerViewAdapter.LatestViewHolder>{
    private ImageLoader imageLoader;
    private List<Latest.DataEntity.EntryEntity> lde;

    public LatestRecyclerViewAdapter(Context c) {
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(List<Latest.DataEntity.EntryEntity> datas) {
        this.lde = datas;
        notifyDataSetChanged();
    }

    @Override
    public LatestViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.latest_recyclerview_item, null);
        return new LatestViewHolder(v);
    }

    @Override
    public void onBindViewHolder(LatestViewHolder holder, int position) {
        if (lde != null && lde.size() > 0) {
            Latest.DataEntity.EntryEntity l = lde.get(position);
            String imgUrl = l.getAvatar();
            getBitmapCache(imgUrl, holder.latestUserHead_Iv);

//            int a = l.getPublish_time();
//            String str = DataChange.getStrTime(a);
            holder.latestUserName_Tv.setText(l.getUsername());
            holder.latestData_Tv.setText(DataChange.getStrTime(l.getPublish_time() + ""));
            holder.latestCommit_Tv.setText(l.getReplys());
            holder.latestContent_Tv.setText(l.getTitle());
        }
    }

    private void getBitmapCache(String url, ImageView imageView) {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(
                imageView, R.mipmap.ic_qyer_gray_150, R.mipmap.ic_qyer_gray_150
        );
        imageLoader.get(url, listener);
    }

    @Override
    public int getItemCount() {
        return lde != null && lde.size() > 0 ? lde.size() : 0;
    }

    public class LatestViewHolder extends RecyclerView.ViewHolder {
        private TextView latestUserName_Tv, latestContent_Tv, latestData_Tv, latestCommit_Tv;
        private ImageView latestUserHead_Iv;

        public LatestViewHolder(View v) {
            super(v);
            latestUserHead_Iv = (ImageView) v.findViewById(R.id.latestUserHead_Iv);
            latestUserName_Tv = (TextView) v.findViewById(R.id.latestUserName_Tv);
            latestContent_Tv = (TextView) v.findViewById(R.id.latestContent_Tv);
            latestCommit_Tv = (TextView) v.findViewById(R.id.latestCommit_Tv);
            latestData_Tv = (TextView) v.findViewById(R.id.latestData_Tv);
        }
    }
}
